package top.hzy520.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCommands;

import java.io.UnsupportedEncodingException;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 14:09 2018/5/29
 */
@Component
@Slf4j
public class RedisUtil {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
    *@Author: HouZeYu
    *@Description: 设置缓存手机验证码  1 表示验证码未过期 2 表示手机使用次数上限 3 表示设备ip使用次数上限
    *@Date: 14:18 2018/5/29
    */
    public int setSmsVerifyCode(String mobile,String code,String ip,int timeout,String smstype) throws UnsupportedEncodingException {
           Object result1=   redisTemplate.execute((RedisCallback<Integer>) redisConnection->{
            JedisCommands commands= (JedisCommands) redisConnection.getNativeConnection();
             String ipSendCount=commands.get(ip);
             if (ipSendCount!=null&&Integer.parseInt(ipSendCount)>10){
                 log.error("设备ip超过使用次数");
                 return 3;
             }
       String mobileSendCount= commands.get(mobile+smstype);
         if (mobileSendCount!=null&&Integer.parseInt(mobileSendCount)>10){
             log.error("手机号超过使用次数");
             return 2;
         }
       long result=commands.setnx(mobile,code);
         if (result!=1){
             return 1;
         }
         commands.expire(mobile,timeout);
           Long ipVal=commands.incr(ip);
         if (ipVal==1){
            commands.expire(ip,86400);
         }
         Long mobileVal=commands.incr(mobile+smstype);
         if (mobileVal==1){
             commands.expire(mobile+smstype,86400);
         }
         return 0;
            });
          int  voderesult= (int) result1;
           return voderesult;
    }

    public String getMobileVerifyCode(String mobile) {
        Object result1=   redisTemplate.execute((RedisCallback<String>) redisConnection->{
            JedisCommands commands= (JedisCommands) redisConnection.getNativeConnection();
              return commands.get(mobile);
        });
        return String.valueOf(result1);
    }

    public void setEmailVerifyCode(String email, String code, int timeout) {
      Object result=redisTemplate.execute((RedisCallback<String>) redisConnection->{
            JedisCommands commands= (JedisCommands) redisConnection.getNativeConnection();
            commands.setnx(email,code);
            commands.expire(email,timeout);
            return  "ok";
        });
    }

    public String getEmailVerifyCode(String email) {
        Object result=redisTemplate.execute((RedisCallback<String>) redisConnection->{
            JedisCommands commands= (JedisCommands) redisConnection.getNativeConnection();
            return commands.get(email);
        });
        return String.valueOf(result);
    }
}